//
//  HomeNearAdderViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/6/5.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "HomeNearAdderViewController.h"
#import <MJRefresh.h>
#import "PersonNetWork.h"
#import "SZLAccountHelper.h"
#import "NearAdderTableViewCell.h"
#import "MBProgressHUD+MJ.h"
#import "ConEditCardViewController.h"

@interface HomeNearAdderViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *sources;


@end

@implementation HomeNearAdderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"附近合伙人";
    self.curPage = 1;
    [self.view addSubview:self.tableView];
    [self getNearAddList];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearAdderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NearAdderTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdderModel *model = self.sources[indexPath.row];
    [self getAdderInfo:model];
}
- (void)getAdderInfo:(AdderModel *)model
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"mobilePhone"] = model.mobilePhone;
    __WEAK
    [self showWaiter];
    [PersonNetWork getSaoCardInfoWithParame:parame response:^(BOOL success, NSString *msg, CardInfoModel *info) {
        [self hideWaiter];
        if (success) {
            if (model.allowStutas == 1) {
                [MBProgressHUD showError:@"合伙人不允许查看名片"];
                return ;
            }else {
                if (info.cardPhone) {
                    ConEditCardViewController *editVc = [[ConEditCardViewController alloc]init];
                    editVc.type = EditTypeAddFriends;
                    editVc.info = info;
                    editVc.friendsPhone = model.mobilePhone;
                    [weakSelf.navigationController pushViewController:editVc animated:YES];
                }else {
                    [MBProgressHUD showError:@"该用户暂未编辑名片"];
                }
            }
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}

- (void)getNearAddList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [PersonNetWork getNearAdderWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden = (lists.count < 20);
        if (success) {
            if (self.curPage == 1) {
                [self.sources removeAllObjects];
                if (lists.count == 0) {
                    [self showNoDataView:self.tableView.frame title:@"抱歉,附近暂时没有合伙人"];
                    return ;
                }else{
                    [self hideNoDataView];
                }
            }
            [self.sources addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 80;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NearAdderTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NearAdderTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self getNearAddList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.curPage++;
            [self getNearAddList];
        }];
    }
    return _tableView;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
